// pages/pwd/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    requestUrl: app.globalData.requestUrl,
    userInfo: {
      account:'',
      age:0,
      avatarUrl:'',
      gender:null,
      nickName:'',
      phone:'',
      pwd:''
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that=this;
    wx.request({
      url: app.globalData.requestUrl + '/user/selectById?id='+wx.getStorageSync('user').id,
      method: 'post',
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          avatarUrl: res.data.user.avatarUrl
        });
      }
    });
  },
  ChooseImage() {
    let that = this;
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        wx.uploadFile({
          url: app.globalData.requestUrl + '/user/uploadImage', //接受图片的接口地址
          filePath: res.tempFilePaths[0],
          name: 'file',
          success(res1) {
            let returnData = JSON.parse(res1.data);
            let imgUrl =app.globalData.requestUrl+ returnData.imgUrl;
            that.data.userInfo.avatarUrl=imgUrl;
            that.setData({
              userInfo: userInfo
            })
          }
        })
      
      }
    });
  },
  accountBind(e){
    this.data.userInfo.account = e.detail.value;
    this.setData({
      userInfo:this.data.userInfo
    });
  },
  nicknameBind(e){
    this.data.userInfo.nickName = e.detail.value;
    this.setData({
      userInfo:this.data.userInfo
    });
  },
  pwdBind(e){
    this.data.userInfo.pwd = e.detail.value;
    this.setData({
      userInfo:this.data.userInfo
    });
  },
  phoneBind(e){
    this.data.userInfo.phone = e.detail.value;
    this.setData({
      userInfo:this.data.userInfo
    });
  },
  ageBind(e){
    this.data.userInfo.age = e.detail.value;
    this.setData({
      userInfo:this.data.userInfo
    });
  },
  onGenderChange(e){
    this.data.userInfo.gender = e.detail.value;
    this.setData({
      userInfo:this.data.userInfo
    });
  },
  send(){
    let that = this;
    let param = that.data.userInfo;
    wx.request({
      url: app.globalData.requestUrl + '/user/update?userId='+wx.getStorageSync('user').id,
      method: 'post',
      data: param,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.showToast({
          title: '修改成功',
          icon: 'success'
        })
        that.setData({
          userInfo: res.data.user
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(){
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})